package com.gidgabs.modules.api.service.impl;

import com.gidgabs.common.utils.Assist;
import com.gidgabs.common.utils.LangUtil;
import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.dto.TbAddressDto;
import com.gidgabs.modules.api.service.TbAddressService;
import com.gidgabs.modules.dao.TbAddressDao;
import com.gidgabs.modules.entity.TbAddressEntity;
import com.gidgabs.utils.SessionDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TbAddressServiceImpl implements TbAddressService {
	@Autowired
	private TbAddressDao addressDao;
	
	@Override
	public TbAddressEntity queryObject(Integer id){
		return addressDao.queryObject(id);
	}
	
	@Override
	public List<TbAddressEntity> queryList(Map<String, Object> map){
		return addressDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return addressDao.queryTotal(map);
	}
	
	@Override
	public void save(TbAddressEntity address){
		//如何id存在则修改 反之做新增
		if(address.getId() != null){
			addressDao.update(address);
		}else{
			addressDao.save(address);
		}
	}
	
	@Override
	public void update(TbAddressEntity address){
		addressDao.update(address);
	}
	
	@Override
	public void delete(Integer id){
		addressDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Integer[] ids){
		addressDao.deleteBatch(ids);
	}

	@Override
	public ReturnJson<TbAddressDto> queryAdvertiseList(Integer pageSize, Integer pageNo) {
		Assist assist = new Assist();
		assist.andEq("user_id",SessionDataUtil.getCurrentUserId());
		assist.setRowSize(pageSize);
		assist.setStartRow(LangUtil.firstLimit(pageSize, pageNo));
		List<TbAddressEntity> tbProductEntities = addressDao.selectTbAddress(assist);
		long count = addressDao.getTbAddressRowCount(assist);
		List<TbAddressDto> tbAddressDtoList = new ArrayList<>();
		for (TbAddressEntity asd : tbProductEntities) {
			TbAddressDto tbUserAdvertiseDto = asd.toDto();
			tbAddressDtoList.add(tbUserAdvertiseDto);
		}
		return ReturnJson.success("查询成功", tbAddressDtoList, count, pageSize, pageNo);
	}

	@Override
	public TbAddressEntity selectTbAddressObjSingle(TbAddressEntity obj) {
		return addressDao.selectTbAddressObjSingle(obj);
	}

	@Override
	public List<TbAddressEntity> selectTbAddressByObj(TbAddressEntity obj) {
		return addressDao.selectTbAddressByObj(obj);
	}

}
